package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.OrderDetail;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.mapper.OrdersMapper;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.UserService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author fengzhu
 * @since 2024-10-10
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private UserService userService;
    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //分页构造器
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        //条件构造器
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,currentId);
        //进行分页查询
        ordersService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    @GetMapping("/page")
    public R<Page> Page(int page,int pageSize){
        //分页构造器
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage=new Page<>();
        //条件构造器
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByDesc(Orders::getCheckoutTime);
        //进行分页查询
        ordersService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Orders> records = pageInfo.getRecords();
        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto=new OrdersDto();
            BeanUtils.copyProperties(item,ordersDto);
            String number = item.getNumber();
            //根据id进行订单详情对象
            LambdaQueryWrapper<OrderDetail> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.eq(OrderDetail::getNumber,number);
            List<OrderDetail> list1 = orderDetailService.list(queryWrapper1);
            if(list1!=null){
                User user = userService.getById(item.getUserId());
                ordersDto.setUserName(user.getName());
                ordersDto.setPhone(item.getPhone());
                ordersDto.setConsignee(item.getConsignee());
                ordersDto.setAddress(item.getAddress());
                ordersDto.setOrderDetails(list1);
            }

            return ordersDto;
        }).collect(Collectors.toList());
        log.info("{}",list);
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
    @PutMapping()
    public  R<String> changestatus(@RequestBody  Orders orders){
        log.info(orders.toString());
        int status = orders.getStatus();

        ordersService.updateById(orders);
        if (status==3){
            return  R.success("订单已派送");
        }

        return  R.success("订单已完成");


    }
}
